/*
  Copyright 2002-2003 The University of Texas at Austin
  
    Authors: Anthony Thane <thanea@ices.utexas.edu>
             Vinay Siddavanahalli <skvinay@cs.utexas.edu>
    Advisor: Chandrajit Bajaj <bajaj@cs.utexas.edu>

  This file is part of Volume Rover.

  Volume Rover is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  Volume Rover is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with iotree; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <QFile>

#include "IsocontourMap.h"

using namespace PBVP;

int IsocontourMapNode::ms_NextID = 0;

IsocontourMap::IsocontourMap(int maxNumOfNodes)
{
	m_MaxIsoNodes = maxNumOfNodes;
	//AddNode( 0.5 );
	//AddNode( 0.6 );
}

IsocontourMap::IsocontourMap(const IsocontourMap& copy) 
{
	m_ContourNodes = copy.m_ContourNodes;
}


IsocontourMapNode::IsocontourMapNode(double pos) 
: position(pos)
{
	id = getNextID();
}

IsocontourMapNode::IsocontourMapNode(const IsocontourMapNode& copy)
: position(copy.position), id(copy.id)
{}

int IsocontourMap::AddNode(double pos)
{
	assert(pos >= 0.0 && pos <= 1.0);
	if(m_ContourNodes.size() >= m_MaxIsoNodes) return -1;

	IsocontourMapNode node(pos);
	m_ContourNodes.append(node);
	return node.id;
}

void IsocontourMap::DeleteIthNode(int index)
{
	m_ContourNodes.removeAt(index);
}

void IsocontourMap::MoveIthNode(int index, double pos)
{
	if(pos > 1.0) { pos = 1.0; }
	if(pos < 0.0) { pos = 0.0; }
	m_ContourNodes[index].position = pos;
}

int IsocontourMap::GetIDofIthNode(int index) const
{
	return m_ContourNodes[index].id;
}

void IsocontourMap::ChangeColor(int index, double fR, double fG, double fB)
{
	m_ContourNodes[index].color = Color(fR, fG, fB);
}

double IsocontourMap::GetRed(int index)
{
	return m_ContourNodes[index].red();
}

double IsocontourMap::GetGreen(int index)
{
	return m_ContourNodes[index].green();
}

double IsocontourMap::GetBlue(int index)
{
	return m_ContourNodes[index].blue();
}

int IsocontourMap::size() const
{
	return m_ContourNodes.size();
}

double IsocontourMap::GetPosition(int id) const
{
	for(int i = 0; i < size(); i++) {
		if(m_ContourNodes[i].id == id) {
			return m_ContourNodes[i].position;
		}
	}
	return 0.0;
}

double IsocontourMap::GetPositionOfIthNode(int index) const
{
	return m_ContourNodes[index].position;
}

/********************

IsocontourMap
No of nodes
< no of nodes >
Position
< Position A1 >
< Position A2 >
< Position A3 >
< Position A4 >
...
< Position AN >

*********************/
void IsocontourMap::saveMap( QTextStream& stream )
{
	stream << "IsocontourMap\n";
	stream << "Number of nodes\n";
	stream << size() << "\n";
	
	stream << "Position\n";

	for( int i=0; i < size(); i++ )
	{
		stream << m_ContourNodes[i].position << "\n";
	}
}

void IsocontourMap::loadMap( QTextStream& stream )
{
	double position;
	int size;
	QString junk;

	m_ContourNodes.clear();

	stream.skipWhiteSpace();
	junk = stream.readLine(); // IsocontourMap

	stream.skipWhiteSpace();
	junk = stream.readLine(); // Number of nodes
	stream >> size;

	stream.skipWhiteSpace();
	junk = stream.readLine(); // Position
	for(int i = 0; i < size; i++) {
		stream >> position;
		m_ContourNodes.append(IsocontourMapNode(position));
	}
}

